# -*- coding: utf-8 -*-
import pymysql
import contextlib


@contextlib.contextmanager
def mysql(host="127.0.0.1", port=3306, user="root", passwd="", db="typecho",charset="utf8"):
    con = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset)
    cursor = con.cursor(cursor=pymysql.cursors.DictCursor)
    try:
        yield cursor
    finally:
        con.commit()
        cursor.close()
        con.close()